package com.psa.element;

import android.content.ContentValues;
import android.database.Cursor;
import com.google.gson.Gson;
import com.psa.tool.DBTool;

/**
 *
 * @author david
 * @date 2014/5/14
 */
public class Merchandise implements IJson,ISqlObject  {
    public String ID = "";
    public String NameChinese = "";
    public String PackingNameChinese1 = "";
    public String PackingNameChinese2 = "";
    public float Price = 1.00f;
    public int Packing = 1;
    public String Brand = "";
    public String Package = "";

    private ContentValues createValues() {
        ContentValues values = new ContentValues();
        values.put("NameChinese", NameChinese.trim());
        values.put("PackingNameChinese1", PackingNameChinese1.trim());
        values.put("PackingNameChinese2", PackingNameChinese2.trim());
        values.put("Price", Price);
        values.put("Packing", Packing);
        values.put("Brand", Brand.trim());
        values.put("Package", Package.trim());
        return values;
    }

    @Override
    public String toJson() {
        return new Gson().toJson(this);
    }

    @Override
    public int insert(DBTool dbTool) {
        ContentValues values = createValues();
        values.put("ID", ID.trim());
        return (int)dbTool.insert("Merchandise", values);
    }

    @Override
    public int update(DBTool dbTool) {
        ContentValues values = createValues();
        String where = "ID = ?";
        String[] whereval = { ID.trim() };
        return dbTool.update("Merchandise", values, where, whereval);
    }

    @Override
    public int delete(DBTool dbTool) {
        String where = "ID = ?";
        String[] wherevalue = { ID.trim() };
        return dbTool.delete("Merchandise", where, wherevalue);
    }

    @Override
    public ISqlObject full(Cursor cursor) {
        if (cursor!=null && cursor.moveToNext()) {
            Merchandise merchandise = new Merchandise();
            merchandise.ID = cursor.getString(0).trim();
            merchandise.NameChinese = cursor.getString(1);
            merchandise.PackingNameChinese1 = cursor.getString(2);
            merchandise.PackingNameChinese2 = cursor.getString(3);
            merchandise.Price = cursor.getFloat(4);
            merchandise.Packing = cursor.getInt(5);
            merchandise.Brand = cursor.getString(6);
            merchandise.Package = cursor.getString(7);
            return merchandise;
        }
        return null;
    }

    @Override
    public void clear(DBTool dbTool) {
        dbTool.delete("Merchandise", "ID Like ?", new String[]{"%"});
    }

    @Override
    public String getDataSql() {
        return "Merchandise";
    }

    @Override
    public String getSyncMethod() {
        return "getdataobjects";
    }

    @Override
    public String toString() {
        return ID + " " + NameChinese.replace('|', '\n');
    }

    public String getNameChinese(){
        return NameChinese.replace('|', '\n');
    }

    public static Merchandise getMerchandise(String id){
        Merchandise merchandise = new Merchandise();
        Cursor cursor = DBTool.getInstance().query("Select * From Merchandise Where trim(ID)='"+ id.trim() + "'");
        ISqlObject iSqlObject = merchandise.full(cursor);
        merchandise = iSqlObject!=null?(Merchandise)iSqlObject:null;
        return merchandise;
    }
}
